package service

import (
	"context"
	"go.uber.org/zap"
	"golang-sgg-daijia/internal/repository/minio"
	"mime/multipart"
)

type FileService interface {
	Upload(ctx context.Context, file *multipart.FileHeader) (string, error)
}

func NewFileService(
	service *Service,
	minioClient minio.MinioClient,
) FileService {
	return &fileService{
		Service:               service,
		minioClient:           minioClient,
	}
}

type fileService struct {
	*Service
	minioClient           minio.MinioClient
}

func (s *fileService) Upload(ctx context.Context, file *multipart.FileHeader) (string, error) {
	f, err := file.Open()
	if err != nil {
		s.logger.WithContext(ctx).Error("fileService|Upload|Open", zap.Error(err))
		return "", err
	}
	url, err := s.minioClient.PutObject(ctx, file.Filename, f, file.Size)
	if err != nil {
		return "", err
	}

	return url, nil
}
